/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cult.ticketing.dao;

import cult.ticketing.jdbc.DB;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
 *
 * @author yo
 */
public class PurchaseRecordDAO {

    private final int NON_MEMBER_ID = -1;
    private final int NO_REFRUND_REQUEST = -1;
    
    public boolean addPurchaseRecord(int memberId, String email, int scheduleItemId, String seatIndex, String purchaseTime) {
        boolean isSuccessful = true;
        String[] seatArr = seatIndex.split(";");
        int refundStatus = NO_REFRUND_REQUEST;


        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO purchase_record([member_id], [email], [scheduleItem_id], [seat_index], [refund_status], [purchase_time])VALUES(?, ?, ?, ?, ?, ?);");
            pstmt.setInt(1, memberId);
            pstmt.setString(2, email);
            pstmt.setInt(3, scheduleItemId);
            pstmt.setInt(5, refundStatus);
            pstmt.setString(6, purchaseTime);

            for (int i=0; i<seatArr.length; i++) {
                 pstmt.setString(4, seatArr[i]);
                 int r = pstmt.executeUpdate();
                 if (r <= 0) {
                    isSuccessful = false;
                }
            }

            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return isSuccessful;
    }
}
